﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace GraphicsEditor
{
    public partial class BrushDialog : Form
    {
        private ShapeBrush brush = new ShapeBrush();

        public ShapeBrush Brush
        {
            get
            {
                return brush;
            }
            set
            {
                brush = value;
            }
        }

        public BrushDialog(ShapeBrush brush)
        {
            InitializeComponent();
            InitializeComboBoxes();
            InitControls(brush);
        }

        private void InitializeComboBoxes()
        {
            typeComboBox.DataSource = Enum.GetValues(typeof(FillType));
            patternComboBox.DataSource = Enum.GetValues(typeof(HatchStyle));
            viewComboBox.DataSource = Enum.GetValues(typeof(GraphicsEditor.PreviewView));
        }

        private void previewPanel_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.FillRectangle(brush.GetBrush(), ClientRectangle);
        }


        /// <summary>
        /// Update all control items according to current brush
        /// </summary>
        private void InitControls(ShapeBrush brs)
        {
            if (brs.FillType == FillType.Solid)
            {
                EnableHatchControlSettings(false);                
            }
            else // hatch brush
            {
                EnableHatchControlSettings(true);
            }
            typeComboBox.SelectedItem = brs.FillType;
            patternComboBox.SelectedItem = brs.HatchStyle;
            foreColorButton.BackColor = brs.ForeColor;
            foreAlphaNumericUpDown.Value = brs.ForeColor.A;
            backColorButton.BackColor = brs.BackColor;
            backAlphaNumericUpDown.Value = brs.BackColor.A;
            brush = brs;
            previewPanel.Refresh();
        }

        private void viewComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            GraphicsEditor.PreviewView viewBackground = (GraphicsEditor.PreviewView)viewComboBox.SelectedValue;
            switch (viewBackground)
            {
                case GraphicsEditor.PreviewView.Canvas:
                    previewPanel.BackgroundImage = null;
                    previewPanel.BackColor = GraphicsEditor.CANVAS_COLOR;
                    break;
                case GraphicsEditor.PreviewView.Checkerboard:
                    previewPanel.BackgroundImage = Properties.Resources.checkerboard;
                    break;
            }
        }

        private void typeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            brush.FillType = (FillType)typeComboBox.SelectedItem;
            EnableHatchControlSettings(brush.FillType == FillType.Hatch);
            
        }

        private void EnableHatchControlSettings(bool isEnabled)
        {
            patternLabel.Enabled = isEnabled;
            patternComboBox.Enabled = isEnabled;

            backColorLabel.Enabled = isEnabled;
            backColorButton.Enabled = isEnabled;

            backAlphaLabel.Enabled = isEnabled;
            backAlphaNumericUpDown.Enabled = isEnabled;
        }

        private void foreColorButton_Click(object sender, EventArgs e)
        {
            ColorDialog colorDialog = new ColorDialog();
            if (colorDialog.ShowDialog() == DialogResult.OK)
            {
                brush.ForeColor = colorDialog.Color;
                foreColorButton.BackColor = brush.ForeColor;
                previewPanel.Refresh();
            }
        }

        private void backColorButton_Click(object sender, EventArgs e)
        {

            ColorDialog colorDialog = new ColorDialog();
            if (colorDialog.ShowDialog() == DialogResult.OK)
            {
                brush.BackColor = colorDialog.Color;
                backColorButton.BackColor = brush.BackColor;
                previewPanel.Refresh();
            }
        }



        private void patternComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            brush.HatchStyle = (HatchStyle)patternComboBox.SelectedItem;
            previewPanel.Refresh();
        }

        private void foreAlphaNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            brush.ForeColor = Color.FromArgb((int)foreAlphaNumericUpDown.Value, brush.ForeColor);
            previewPanel.Refresh();
        }

        private void backAlphaNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            brush.BackColor = Color.FromArgb((int)backAlphaNumericUpDown.Value, brush.BackColor);
            previewPanel.Refresh();
        }


    }
}

